<?php
namespace app\admin\controller;

class Example extends Base
{
    public function index()
    {
    	if (!empty(input("get.start_time")) && !empty(input("?get.end_time"))) {
            $start_time = strtotime(input("get.start_time"));
            $end_time = strtotime(input("get.end_time")) + 86400;
            if ($end_time > $start_time) {
                $where["add_time"] = ["between",$start_time.','.$end_time];
                $this->assign("start_time",input("get.start_time"));
                $this->assign("end_time",input("get.end_time"));
            }
        }

        if (input("get.category")) {
            $where["category"] = input("get.category");
            $this->assign("category",input("get.category"));
        }else{
            $this->assign("category",'');
        }
        $where["id"] = ["gt",0]; 
    	$count = db('example')->where($where)->count();
    	$page = new \think\Page($count,10);
		$list = db('example')->where($where)->order("id desc")->limit($page->firstRow.','.$page->listRows)->select();
		foreach ($list as $key => $value) {
			$list[$key]["add_time"] = date("Y-m-d H:i:s",$value["add_time"]);
            $list[$key]["update_time"] = date("Y-m-d H:i:s",$value["update_time"]);
		}
        $categorys = db("example")->field("category")->group("category")->select();
		$page = $page->show();
		$this->assign('list', $list);
		$this->assign('page', $page);
		$this->assign('categorys', $categorys);
		return $this->fetch();
    }

    public function add()
    {
        if (request()->isPost()) {


            // //上传文件
            // if (strpos($_FILES["image"]["type"],"image") !== false) {
            //     $add['image'] = fileUpLoad($_FILES["image"]);
            // }

            // //上传多个文件
            // foreach ($_FILES["actor"]["name"] as $key => $value) {
            //     if (strpos($_FILES["actor"]["type"][$key],"image") !== false) {
            //         $add['actor'][] =  fileUpLoad($_FILES["actor"],$key);
            //     }
            // }

            $back = mt_rand(0,1);
            if ($back) {
                $this->success('添加成功', '/admin/example.html');
            }else{
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }

    public function add1()
    {
        if (request()->isPost()) {
            $back = mt_rand(0,1);
            if ($back) {
                $this->success('添加成功', '/admin/example.html');
            }else{
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }

    public function update()
    {
        if (request()->isPost()) {
            $back = mt_rand(0,1);
            if ($back) {
                $this->success('修改成功', '/admin/example.html');
            }else{
                $this->error('修改失败');
            }
        }
        return $this->fetch();
    }

    public function delete()
    {
        $back = mt_rand(0,1);
        if ($back) {
            $array = ["status" => 1,"message" => "删除成功"];
        }else{
            $array = ["status" => 0,"message" => "删除失败"];
        }
        
        return json($array);
    }

    public function top()
    {
        $back = mt_rand(0,1);
        if ($back) {
            $array = ["status" => 1,"message" => "修改成功"];
        }else{
            $array = ["status" => 0,"message" => "修改失败"];
        }
        return json($array);
    }

    public function cancel()
    {
        $back = mt_rand(0,1);
        if ($back) {
            $array = ["status" => 1,"message" => "修改成功"];
        }else{
            $array = ["status" => 0,"message" => "修改失败"];
        }
        return json($array);
    }

}
